package com.houxm.jbapu.pool;

import java.sql.Connection;
import java.sql.SQLException;

import javax.naming.NameAlreadyBoundException;
import javax.naming.NameNotFoundException;

public class ConnectionPoolManager {

	public static ConnectionPool build(String name, String driver, String url, String user, String password) throws NameAlreadyBoundException, ClassNotFoundException, IllegalAccessException, InstantiationException, SQLException {
		ConnectionParam param = new ConnectionParam(name, driver, url, user, password);
		ConnectionPool cp = ConnectionPoolFactory.newInstance(param);
		return cp;
	}
	
	public static ConnectionPool buildDefaultPool(String driver, String url, String user, String password) throws NameAlreadyBoundException, ClassNotFoundException, IllegalAccessException, InstantiationException, SQLException {
		ConnectionParam param = new ConnectionParam("defaultPool", driver, url, user, password);
		ConnectionPool cp = ConnectionPoolFactory.newInstance(param);
		return cp;
	}
	
	public static ConnectionPool getDefaultPool() throws NameNotFoundException {
		return ConnectionPoolFactory.lookup("defaultPool");
	}
	
	public static Connection getDefaultConn() throws NameNotFoundException, SQLException {
		ConnectionPool cp = ConnectionPoolFactory.lookup("defaultPool");
		return cp.getConnection();
	}
}
